---
title: "Lab 7 Code"
author: "Fabio Setti"
date: "Spring-2025"
output: html_document
---

## Today's Packages and Data 🤗


```{r}
install.packages("dominanceanalysis")
install.packages("pwr")
# install.packages("tidyverse")
```


```{r}
library(dominanceanalysis)
library(pwr)
library(tidyverse)
theme_set(theme_classic(base_size = 14, 
                        base_family = 'serif'))
```


```{r}
#| warning: false
#| classes: code-125
#| echo: true
#| code-line-numbers: false
#| output: true

music <- rio::import("https://github.com/quinix45/PSYC-7804-Regression-Lab-Slides/raw/refs/heads/main/Slides%20Files/Data/music.csv") %>% 
  na.omit()

str(music, 
    give.attr = FALSE)
```



## Regression coefficients Weirdness 


```{r}
round(cor(music[,1:3]), 2)
```

```{r}
reg_coll <- lm(Intense ~ Advnt + Intel, data = music)

# car function for shorter regression summary 
car::S(reg_coll, brief = TRUE)
```



## Visulazing Multicollinearity


```{r}

source("https://github.com/quinix45/PSYC-7804-Regression-Lab-Slides/raw/refs/heads/main/Slides%20Files/R_scripts/3D_plot.R")

nice_3D_plot(y = music$Intense,
             x1 = music$Advnt,
             x2 = music$Intel,
             axis_names = c("Intese", 
                            "Adventurousness", 
                            "Intellect"),
             reg_plane = TRUE)

```


## Back to Semi-Partial Correlations



```{r}
ppcor::spcor(music[,1:3])[1]
```


```{r}
cor(music[,1:3])[1,]
```

## Variance Inflation Factor


```{r}
car::vif(reg_coll)
```



## Another Perspective: Residuals

```{r}
resid_Advnt <- residuals(lm(Advnt ~ Intel, music))

var(resid_Advnt)
```

```{r}
resid_Intel <- residuals(lm(Intel ~ Advnt, music))

var(resid_Intel)
```

## Example with our Data

```{r}
reg_da <- lm(Intense ~ Advnt + Intel + Uncon, music) 
DA <- dominanceAnalysis(reg_da)
summary(DA)
```



```{r}
summary(reg_da)$r.squared
```


## Additional Dominance Patterns


```{r}
dominanceMatrix(DA, type = "conditional")
```

```{r}
dominanceMatrix(DA, type = "complete")
```




## Uncertainty and Inference in DA


```{r}
set.seed(2341)
DA_boot <- bootDominanceAnalysis(reg_da, R = 1000)
summary(DA_boot)
```


## Power for Correlation

```{r}
# we'll need 85 participants (you round up n)
pwr::pwr.r.test(r = .3,
                sig.level = .05,
                power = .8)
```


```{r}
# around .41 (41%) power
pwr::pwr.r.test(r = .2,
                sig.level = .05,
                n = 75)
```



## Power For $R^2$

```{r}
(f2 <- .15/(1-.15))


pwr::pwr.f2.test(u = 3,  f2 = f2, 
                 sig.level = 0.05, power = 0.8)
```


## Power For $\Delta R^2$


```{r}
#.05 increase in R^2

(f2 <- .05/(1-.20))

pwr::pwr.f2.test(u = 1,  f2 = f2, 
                 sig.level = 0.05, power = 0.8)
```



## Power by hand


```{r}
(crit_val <- qf(.95, df1 = 3, df2 = 96))

f2 <- .15/(1-.15)
L_par <- f2*100
# ncp = is the non-centrality parameter argument. It defaults to 0
pf(crit_val, df1 = 3, df2 = 96, ncp = L_par, 
   lower.tail = FALSE)
```


## Checking with the `pwr.f2.test()` function


```{r}
pwr::pwr.f2.test(u = 3, v = 96, f2 = f2)
```

